package club.xiaojiawei.binarytree;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author 肖嘉威
 * @version 1.0
 * @date 5/11/22 10:49 PM
 * @question 111. 二叉树的最小深度
 * @description 给定一个二叉树，找出其最小深度。
 * 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
 * 说明：叶子节点是指没有子节点的节点。
 */
public class MinDepth111 {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        TreeNode right = new TreeNode(20);
        right.left = new TreeNode(15);
        right.right = new TreeNode(7);
        root.right = right;
        int result = minDepth(root);
        System.out.println(result);
    }

    /**
     * BFS+队列
     * 思路：判断遍历到的节点是否是叶子节点，如果是，直接返回遍历到的深度
     * @param root
     * @return
     */
    public static int minDepth(TreeNode root) {
        if (root == null){
            return 0;
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int size, count = 0;
        while ((size = queue.size()) > 0){
            count++;
            for (int i = 0; i < size; i++) {
                TreeNode current = queue.pop();
                if (current.left != null){
                    queue.add(current.left);
                    if (current.right != null){
                        queue.add(current.right);
                    }
                }else if (current.right == null){
                    return count;
                }else {
                    queue.add(current.right);
                }
            }
        }
        return count;
    }

    /**
     * 递归+DFS
     * @param root
     * @return
     */
    public static int minDepth2(TreeNode root){
        if (root == null){
            return 0;
        }else if (root.left == null && root.right == null){
            return 1;
        }
        int left = minDepth2(root.left);
        int right = minDepth2(root.right);
//        如果该节点只有一边有节点，则返回有节点的那一边的深度
        if (left == 0){
            return right + 1;
        }else if (right == 0){
            return left + 1;
        }
//        如果该节点两边都有节点，则返回两边深度小的一边
        return Math.min(left, right) + 1;
    }

    /**
     * 民间-BFS+队列(推荐)
     * @param root
     * @return
     */
    public int minDepth3(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        int level = 1;
        while (!deque.isEmpty()) {
            int size = deque.size();
            for (int i = 0; i < size; i++) {
                TreeNode cur = deque.poll();
                if (cur.right == null && cur.left == null){
                    return level;
                }
                if (cur.left!=null){
                    deque.offer(cur.left);
                }
                if (cur.right!=null){
                    deque.offer(cur.right);
                }
            }
            level++;
        }
        return level;
    }

    static class TreeNode{

        private int val;

        private TreeNode left;

        private TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public TreeNode getLeft() {
            return left;
        }

        public void setLeft(TreeNode left) {
            this.left = left;
        }

        public TreeNode getRight() {
            return right;
        }

        public void setRight(TreeNode right) {
            this.right = right;
        }
    }
}
